/*
 * @(#)SysconfigFilter.java   1.0  2021年12月24日
 * 
 * Copyright (c)	2014-2020. All Rights Reserved.	GuangZhou hhmk Technology Company LTD.
 */
package com.swift.dts.filter.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.swift.dts.filter.DtsFilter;
import com.swift.dts.loadconfig.FileSysConfig;
import com.swift.dts.loadconfig.model.DtsDataBase;
import com.swift.dts.model.DtsBinlogMessage;
import com.swift.util.type.TypeUtil;

/**
 * 添加说明 
 * @author zhengjiajin
 * @version 1.0 2021年12月24日
 */
@Component
@Order(1)
public class SysconfigFilter implements DtsFilter {

    @Autowired
    private FileSysConfig fileSysConfig;

    /** 
     * @see com.swift.dts.filter.DtsFilter#filter(com.swift.dts.model.DtsBinlogMessage)
     */
    @Override
    public boolean filter(DtsDataBase todb, DtsBinlogMessage message) {
        if(!message.getIsDdl()) return false;
        if(TypeUtil.isNull(message.getSql())) return false;
        String sql = message.getSql().toLowerCase().trim();
        boolean allowDrop = fileSysConfig.read().getAllowDrop();
        if(!allowDrop) {
            if(sql.startsWith("drop")) return true;
        }
        
        boolean allowTruncate = fileSysConfig.read().getAllowTruncate();
        if(!allowTruncate) {
            if(sql.startsWith("truncate")) return true;
        }
        return false;
    }
}
